package com.youtellv1.mapper;

import com.youtellv1.dto.GSalDTO;
import com.youtellv1.entity.GSal;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GSalMapper extends BaseMapper<GSal>{
    @Select("select count(*) from tb_gsal where tid = #{tid}")
    int countByTid(@Param("tid") Integer tid);

    @Select("SELECT g.*, t.tname, t.tsex, t.tage, t.tphone " +
            "FROM tb_gsal g " +
            "LEFT JOIN tb_teachers t ON g.tid = t.tid " +
            "WHERE g.tid = #{tid} " +
            "ORDER BY g.month DESC " +
            "LIMIT #{offset}, #{size}")
    List<GSalDTO> findByTidWithPage(@Param("tid") Integer tid,
                                    @Param("offset") int offset,
                                    @Param("size") int size);

    @Insert("insert into tb_gsal(base_sal, subsidy, edu, full_time, phone, health, safe, class_hour, parent, num, year, comm, post_sal, month, tid) values (#{base_sal}, #{subsidy}, #{edu}, #{full_time}, #{phone}, #{health}, #{safe}, #{class_hour}, #{parent}, #{num}, #{year}, #{comm}, #{post_sal}, #{month}, #{tid})")
    void insert(GSalDTO gsaldto);

    @Select("select * from tb_gsal where sid = #{sid}")
    GSal findById(@Param("sid") Integer sid);

    @Update("<script>" +
            "update tb_gsal " +
            "<set>" +
            "    <if test='base_sal != null'>base_sal = #{base_sal},</if>" +
            "    <if test='subsidy != null'>subsidy = #{subsidy},</if>" +
            "    <if test='edu != null'>edu = #{edu},</if>" +
            "    <if test='full_time != null'>full_time = #{full_time},</if>" +
            "    <if test='phone != null'>phone = #{phone},</if>" +
            "    <if test='health != null'>health = #{health},</if>" +
            "    <if test='safe != null'>safe = #{safe},</if>" +
            "    <if test='class_hour != null'>class_hour = #{class_hour},</if>" +
            "    <if test='parent != null'>parent = #{parent},</if>" +
            "    <if test='num != null'>num = #{num},</if>" +
            "    <if test='year != null'>year = #{year},</if>" +
            "    <if test='comm != null'>comm = #{comm},</if>" +
            "    <if test='post_sal != null'>post_sal = #{post_sal},</if>" +
            "    <if test='month != null'>month = #{month},</if>" +
            "    <if test='tid != null'>tid = #{tid}</if>" +
            "</set>" +
            "where sid = #{sid}" +
            "</script>")
    void update(GSalDTO gSalDTO);

    @Delete("delete from tb_gsal where sid = #{sid}")
    void delete(@Param("sid") Integer sid);
}
